﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarberShop.Model;
using BarberShop.Data;
using System.Linq.Expressions;
using BarberShop.Common;
using BarberShop.Common.Enum;
namespace BarberShop.BLL
{
    public class UpdateFilesBLL
    {

        public static bool Add(UpdateFiles info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    data.UpdateFiles.InsertOnSubmit(info);
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }
        public static void UpdateFileName(TableType tableid, int fileid,string filename)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.UpdateFiles where a.TableID == (int)tableid && a.FileID == fileid select a).FirstOrDefault();
                if (info != null)
                {
                    info.FilePath = info.FilePath.Replace(info.FileName, filename);
                    info.FileName = filename;
                    data.SubmitChanges();
                }
            }
        }
        public static IList<UpdateFiles> GetUpdateFilesByStoreId(int storeid)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                Expression<Func<UpdateFiles, bool>> WhereExpr = PredicateExtensions.True<UpdateFiles>();
                if (storeid != 0)
                {
                    WhereExpr = WhereExpr.And(x =>x.Stores_ID.Value.Equals(storeid)&&x.Status==(int)FileStatus.Normal);
                }
                var List = data.UpdateFiles.Where(WhereExpr).ToList();
                int? status = (from c in data.StoresInfo select c.VerUpdateStatus).FirstOrDefault();
                if (status.HasValue && status ==(int)FileStatus.Normal)
                {
                    var updatefile = new UpdateFiles();
                    updatefile.FileName = "shop.xml";
                    updatefile.FilePath = "keyner";
                    string UpdateUrl=Maticsoft.Common.ConfigHelper.GetConfigString("UpdateUrl");
                    updatefile.LinkPath = UpdateUrl + "Notice/DownloadFiles.ashx?Type="+((int)TableType.StoreInfoTable)+"&StoreId=" + storeid;
                    List.Insert(0, updatefile);
                }
               
                return List.ToList();
            }
        }

        public static void SetFilesStatus(TableType tableid,int storeid, int fileid, FileStatus status)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                if (tableid != TableType.StoreInfoTable)
                {
                    var info = (from a in data.UpdateFiles where a.TableID == (int)tableid && a.FileID == fileid select a).FirstOrDefault();
                    if (info != null)
                    {
                        info.Status = (int)status;
                        data.SubmitChanges();
                    }
                }
                else
                {
                    StoresInfoBLL.SetStatusDateTime(storeid, FileStatus.Updated,null, DateTime.Now);
                }
            }
        }

        public static int GetFilesStatus(TableType tableid, int fileid)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.UpdateFiles where a.TableID == (int)tableid && a.FileID == fileid select a).FirstOrDefault();
                if (info != null)
                {
                    return info.Status.Value;
                }
                return -1;
            }
        }
        
    }
}
